package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itheima.dao.CheckItemDao;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.pojo.CheckItem;
import com.itheima.service.CheckItemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 检查项服务
 */
@Service(interfaceClass = CheckItemService.class)//加入了事务注解之后,该Service注解需要指定相应的接口类型
@Transactional
public class CheckItemServiceImpl implements CheckItemService {

    //注入DAO对象
    @Autowired
    private CheckItemDao checkItemDao;

    public void add(CheckItem checkItem) {
        checkItemDao.add(checkItem);
    }

    @Override
    public PageResult queryPage(QueryPageBean queryPageBean) {
        //开始分页查询
        PageHelper.startPage(queryPageBean.getCurrentPage(),queryPageBean.getPageSize());
        //调用dao查询
        String queryString = queryPageBean.getQueryString();
        if (queryString != null){
            queryString="%"+queryString+"%";
            queryPageBean.setQueryString(queryString);
        }
        List<CheckItem>checkItems = checkItemDao.queryPage(queryPageBean);
        //把查到的检查项和总数分装成一个PageResult对象返回
        PageInfo<CheckItem> pageInfo = new PageInfo<CheckItem>(checkItems);
        return new PageResult(pageInfo.getTotal(),checkItems);
    }
    //根据ID删除检查项
    @Override
    public void deleteById(Integer id) {
        //判断当前检查项是否已经关联到检查组
        long count = checkItemDao.findCountByCheckItemID(id);
        if (count>0){
            //当前检查项已经被关联到检查组
            new RuntimeException();
        }else {
            checkItemDao.deleteById(id);
        }


    }

    @Override
    public void edit(CheckItem checkItem) {
        checkItemDao.edit(checkItem);
    }

    @Override
    public CheckItem findById(Integer id) {
        return checkItemDao.findById(id);
    }
}
